<?php
    require 'common.php';
    
    $db = new Database();
    $user = getUser($db);
    
    // Error check
    if($user['group'] < 1) {
        redirect('login.php');
        exit();
    }
    if(!isset($_GET['id']))
        throw new Exception('Bad request.');
    $result = $db->query('SELECT question FROM '.
        config('DB_PREFIX').'questions WHERE id='.intval($_GET['id']));
    if(mysqli_num_rows( $result ) < 1)
        throw new Exception('No such question.');
    $row = mysqli_fetch_assoc( $result );
        
    $page = new HtmlPage();
    $page->writeHeader(lang('QUESTION_PAGENAME'));
    $page->writeTitle();
    $page->writeMenu('QUESTION', $user);
    $page->write('<div class="content">', PUSH_TAB);
    $page->write('<h2>'.lang('QUESTION_PAGENAME').'</h2>');
    $page->write('<p>'.htmlEscape($row['question']).'</p>' );
    
    $page->write('<h2>'.lang('ANSWER').'</h2>');
    $page->write('<form action="answer.php" method="post">', PUSH_TAB);
    $page->write(getCsrfGuard());
    $page->write('<input type="hidden" name="id" value="'.intval($_GET['id']).'" />');
    $page->write('<p><textarea name="answer"></textarea></p>');
    $page->write('<p><input type="submit" value="'.lang('SUBMIT_ANSWER').'" /></p>');
    $page->write('</form>', POP_TAB);
    
    $page->write('</div>', POP_TAB);
    $page->writeFooter();
?>